package org.itboys.weixin.controller;

import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.CommonConstants;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.weixin.service.WeixinConfigService;
import org.itboys.weixin.service.WeixinTwoCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/11/1
 * 描述：雄安加盟网_
 */
@Controller
@RequestMapping("/sunday/weixin/ewm")
public class SundayWeixinShareController extends BaseController {
    @Autowired
    private WeixinTwoCodeService codeService;
    @Autowired(required = false)
    private WeixinConfigService weixinConfigService;
    @Autowired
    private SystemDictionaryService dictionaryService;

    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            @RequestParam(value="memberId",required = true) Long memberId,
            Model model){
        model.addAttribute("memberId",memberId);
        String image=codeService.createSceneTwoCode(1l,memberId);
        model.addAttribute("image",image);
        return "/weixin/share";
    }

    @RequestMapping("/ewm")
    public String ewm(
            HttpServletRequest request,
            @RequestParam(value="memberId",required = true) Long memberId,
            Model model){

        String image=codeService.createSceneTwoCode(1l,memberId);
        model.addAttribute("image",image);

        return "/weixin/ewm";
    }

    @RequestMapping("/createSceneTwoCode")
    public void createSceneTwoCode(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="memberId",required = true) Long memberId){

        try{
            String image=codeService.createSceneTwoCode(1l,memberId);
            ResponseMessage.success(image);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code1= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code1= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/ewm/createSceneTwoCode----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code1,msg);
        }
    }

   /* @RequestMapping(value="/test", method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="test")
    public void test(HttpServletRequest request,
                     HttpServletResponse response){
        try{
          *//*  String url= WeixinConstants.URL.FETCH_TOKEN_URL+"&appid=wx5396325b6b627d7a&secret=2677a571c214529573e36dd36aaa7206";
            String jsonToken = HttpHelper.doGet(url);
            logger.info("************jsonToken************"+jsonToken);
            AccessToken token = JsonUtils.jsonToObject(AccessToken.class, jsonToken);
            String accessToken = token.getAccess_token();*//*
            String access_token = WeixinUtils.getAccessToken("wx5396325b6b627d7a", "2677a571c214529573e36dd36aaa7206");
            //SmallCodeUtil.getminiqrQr("1",accessToken);
            JSONObject object=new JSONObject();
            JSONObject data =new JSONObject();
            object.put("touser","otD_Gw-pm9vvBd9-Z7WifYv3Non8");
            object.put("template_id","yr7r7_qwPMKf-KZQxtaqYVmTZAujsKbYlcH8FzWTsds");

            JSONObject firstValue =new JSONObject();
            firstValue.put("value","账户变动提醒");
            data.put("first",firstValue);
            JSONObject keyword1 =new JSONObject();
            keyword1.put("value","Zhome");
            keyword1.put("color","#173177");
            data.put("keyword1",keyword1);

            JSONObject keyword2 =new JSONObject();
            keyword2.put("value","2018-7-2");
            keyword2.put("color","#173177");
            data.put("keyword2",keyword2);
            JSONObject keyword3 =new JSONObject();
            keyword3.put("value","+30");
            keyword3.put("color","#173177");
            data.put("keyword3",keyword3);
            JSONObject keyword4 =new JSONObject();
            keyword4.put("value","推荐奖励");
            keyword4.put("color","#173177");
            data.put("keyword4",keyword4);
            JSONObject keyword5 =new JSONObject();
            keyword5.put("value","110");
            keyword5.put("color","#173177");
            data.put("keyword5",keyword5);
            JSONObject remark =new JSONObject();
            remark.put("value","详情点击查看");
            remark.put("color","#173177");
            data.put("remark",remark);

            object.put("data",data);
            String messageUrl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
            PostMethod postMethod = new PostMethod(messageUrl);
            HttpClient httpclient = HttpHelper.getHttpClient();
            RequestEntity requestEntity = null;
            //JSONObject jsonObject=map2Json(param);
            String result = object.toJSONString();
            System.out.println(result);
            requestEntity = new StringRequestEntity(result, CommonConstants.CONTENT_TYPE.JSON_TYPE ,CommonConstants.ENCODE.UTF_8);
            postMethod.setRequestEntity(requestEntity);
            httpclient.executeMethod(postMethod);
            String createResult = postMethod.getResponseBodyAsString();
            System.out.println(createResult);
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/weixin/code/send------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,e.getMessage()));
        }
    }
*/
}
